package com.nms.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nms.model.Email;
import com.nms.model.TakeUser;
import com.nms.model.Type;
import com.nms.model.User;
import com.nms.service.EmailService;
import com.nms.service.TakeService;
import com.nms.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class SpaceAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	private EmailService emailService;
	private TakeService takeService;
	private UserService userService;
	private Email email;
	private List<Type> untakes;
	private List<TakeUser> utakes;
	private List<Email> emails;
	// Map<String, Object> session =
			// ActionContext.getContext().getSession();
	public String sHomePager() {
		// user = (User) session.get("user");
		emails = takeService.findUTake("aak");
		return "sHomePager";
	}

	public String uMessage() {
		// user = (User) session.get("user");
		emails = emailService.findUEmail("aak");
		return "uMessage";
	}

	public String uTake() {
		// user = (User) session.get("user");
		untakes = takeService.getUNotTakeList("aak");
		utakes = takeService.getUTakeList("aak");
		return "uTake";
	}

	public String uInfo() {
		List<User> users = userService.findAll();
		user = users.get(0);
		return "uInfo";
	}

	@Override
	public String execute() throws Exception {
		// user = (User) session.get("user");
		/* 获得HttpServletRequest对象 */
		HttpServletRequest hsr = ServletActionContext.getRequest();
		/* 获得请求的参数 */
		/* 获得请求的参数 */
		String act = hsr.getParameter("act");
		if (act.equals("saveTake")) {
			String id = hsr.getParameter("id");
			untakes = takeService.getUNotTakeList("aak");
			Type type = untakes.get(Integer.parseInt(id));
			if (takeService.take(type, "aak"))
				return "takeSuccess";
		} else if (act.equals("cancelTake")) {
			String typeStr = hsr.getParameter("type");
			int type = Integer.parseInt(typeStr);
			if (takeService.cancel(type, "aak"))
				return "takeSuccess";
		} else if (act.equals("deleteEmail")) {
			String idStr = hsr.getParameter("id");
			int eid = Integer.parseInt(idStr);
			if (emailService.delete(eid))
				return "emailDetSuccess";
		} else if (act.equals("detailEmail")) {
			String idStr = hsr.getParameter("id");
			int eid = Integer.parseInt(idStr);
			email = emailService.getEmail(eid);
			return "emailDeatil";
		}
		return "error";
	}

	public EmailService getEmailService() {
		return emailService;
	}

	@Resource(name = "emailService")
	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	public TakeService getTakeService() {
		return takeService;
	}

	@Resource(name = "takeService")
	public void setTakeService(TakeService takeService) {
		this.takeService = takeService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Email> getEmails() {
		return emails;
	}

	public void setEmails(List<Email> emails) {
		this.emails = emails;
	}

	public List<Type> getUntakes() {
		return untakes;
	}

	public void setUntakes(List<Type> untakes) {
		this.untakes = untakes;
	}

	public List<TakeUser> getUtakes() {
		return utakes;
	}

	public void setUtakes(List<TakeUser> utakes) {
		this.utakes = utakes;
	}
}
